package com.wordplat.quickstart.xutils.image;

import com.wordplat.quickstart.xutils.ImageManager;
import com.wordplat.quickstart.xutils.common.Callback;
import com.wordplat.quickstart.xutils.x;

import ohos.agp.components.Image;
import ohos.media.image.PixelMap;

import java.io.File;

/**
 * Created by wyouflf on 15/10/9.
 *
 * @since 2021-05-09
 */
public final class ImageManagerImpl implements ImageManager {
    private static final Object lock = new Object();
    private static volatile ImageManagerImpl instance;

    private ImageManagerImpl() {
    }

    /**
     * registerInstance
     */
    public static void registerInstance() {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new ImageManagerImpl();
                }
            }
        }
        x.Ext.setImageManager(instance);
    }

    @Override
    public void bind(final Image view, final String url) {
        x.task().autoPost(new Runnable() {
            @Override
            public void run() {
                ImageLoader.doBind(view, url, null, 0, null);
            }
        });
    }

    @Override
    public void bind(final Image view, final String url, final ImageOptions options) {
        x.task().autoPost(new Runnable() {
            @Override
            public void run() {
                ImageLoader.doBind(view, url, options, 0, null);
            }
        });
    }

    @Override
    public void bind(final Image view, final String url, final Callback.CommonCallback<PixelMap> callback) {
        x.task().autoPost(new Runnable() {
            @Override
            public void run() {
                ImageLoader.doBind(view, url, null, 0, callback);
            }
        });
    }

    @Override
    public void bind(final Image view, final String url, final ImageOptions options, final Callback.CommonCallback<PixelMap> callback) {
        x.task().autoPost(new Runnable() {
            @Override
            public void run() {
                ImageLoader.doBind(view, url, options, 0, callback);
            }
        });
    }

    @Override
    public Callback.Cancelable loadDrawable(String url, ImageOptions options, Callback.CommonCallback<PixelMap> callback) {
        return ImageLoader.doLoadDrawable(url, options, callback);
    }

    @Override
    public Callback.Cancelable loadFile(String url, ImageOptions options, Callback.CacheCallback<File> callback) {
        return ImageLoader.doLoadFile(url, options, callback);
    }

    @Override
    public void clearMemCache() {
        ImageLoader.clearMemCache();
    }

    @Override
    public void clearCacheFiles() {
        ImageLoader.clearCacheFiles();
        ImageDecoder.clearCacheFiles();
    }
}
